package edu.wuster.blog.service.impl;

import edu.wuster.blog.entity.LoginResponse;
import edu.wuster.blog.entity.User;
import edu.wuster.blog.entity.UserResetPassword;
import edu.wuster.blog.exception.MyException;
import edu.wuster.blog.mapper.UserMapper;
import edu.wuster.blog.service.UserService;
import edu.wuster.blog.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        //判断用户是否存在
        if (userMapper.selectByUsername(user) != null) {
            log.info("抛出用户名唯一异常");
            throw new MyException("用户名已存在");
        }
        user.setCreateTime(LocalDate.now());
        userMapper.addUser(user);
    }

    @Override
    public boolean lossPassword(User user) {
        //判断用户是否存在
        if (userMapper.lossPassword(user) == null) {
            log.info("用户验证失败");
            return false;
        }
        log.info("用户验证成功,密码已重置");
        userMapper.resetPassword(user);
        return true;
    }

    @Override
    public User selectById(Integer id) {
        //判断用户是否存在
        if (userMapper.selectById(id) == null) {
            log.info("查看信息用户不存在");
            throw new MyException("查看信息用户不存在");
        }
        log.info("用户（id：{}）查看个人信息", id);
        User user = userMapper.selectById(id);
        if (user != null)
            user.setPassword("******");
        return user;
    }

    @Override
    public void resetById(User user) {
        //判断用户是否存在
        if (userMapper.selectById(user.getId()) == null) {
            log.info("修改信息用户不存在");
            throw new MyException("修改信息用户不存在");
        }
        log.info("用户（id：{}）修改个人信息", user.getId());
        userMapper.resetById(user);
    }

    @Override
    public void resetPassword(UserResetPassword userResetPassword) {
        //判断用户是否存在
        if (userMapper.selectById(userResetPassword.getId()) == null) {
            log.info("修改密码用户不存在");
            throw new MyException("修改密码用户不存在");
        }
        //验证用户身份
        log.info("用户（id：{}）修改密码", userResetPassword.getId());
        if (!userResetPassword.getNewPassword().equals(userResetPassword.getNewPassword2())) {
            log.info("用户两次输入密码不一致");
            throw new MyException("两次输入密码不一致");
        }
        //修改密码
        User user = new User();
        user.setId(userResetPassword.getId());
        user.setPassword(userResetPassword.getNewPassword());
        userMapper.setNewPassword(user);
        log.info("用户修改密码成功");
    }

    @Override
    public void deleteUser(Integer id) {
        if (userMapper.selectById(id) == null) {
            log.info("被注销用户不存在");
            throw new MyException("被注销用户不存在");
        }
        userMapper.deleteUser(id);
    }

    @Override
    public LoginResponse userLogin(User user) {
        User user1 = userMapper.selectLogin(user);
        if (user1 == null)
            throw new MyException("用户名或密码错误");
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user1.getId());
        claims.put("username", user1.getUsername());
        return new LoginResponse(user1.getId(), JWTUtils.getJWT(claims));
    }
}
